﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;

namespace DataAccess_Layer
{
    public class FileWriter
    {
        /// <summary>
        /// Writes the object to a file.
        /// </summary>
        /// <param name="project">Object be saved.</param>
        /// <param name="path">Path to get write the file to.</param>
        public void Write(object project, string path)
        {
            try
            {
                Stream stream = null;
                if (path.EndsWith(".ont")) stream = File.Open(path, FileMode.Create);
                else stream = File.Open(path + ".ont", FileMode.Create);
                BinaryFormatter binaryFormatter = new BinaryFormatter();

                binaryFormatter.Serialize(stream, project);
                stream.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace + "/r/n/r/n");
                Console.WriteLine("Crashed whilst saving file: " + path);
            }
        }

        /// <summary>
        /// Writes a project to HTML.
        /// </summary>
        /// <param name="path">The path to write it to.</param>
        /// <param name="name">A files name.</param>
        /// <param name="html">The HTML file.</param>
        /// <param name="bitmap">A bitmap to save with the HTML doc.</param>
        public void WriteAsHtml(string path, string name, string html, Bitmap bitmap)
        {
            try
            {
                File.WriteAllText(path + name + ".html", html);
                bitmap.Save(path + name + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace + "/r/n/r/n");
                Console.WriteLine("Crashed whilst saving HTML file: " + path);
            }
        }
    }
}
